#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020/4/2 12:26
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 289. 生命游戏.2020-04-02.py
# @Software: PyCharm

__author__ = 'yangdy'

from typing import List


class Solution:
    def gameOfLife(self, board: List[List[int]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        n = len(board)
        if n <= 0:
            return
        m = len(board[0])

        step = [
            (-1, -1), (-1, 0), (-1, 1),
            (0, 1), (0, -1),
            (1, 1), (1, 0), (1, -1),
        ]

        for i in range(n):
            for j in range(m):
                count = 0
                for p in range(8):
                    ii = i + step[p][0]
                    jj = j + step[p][1]
                    if 0 <= ii < n and 0 <= jj < m:
                        count += board[ii][jj] % 2
                if count == 2:
                    board[i][j] += board[i][j] * 2
                elif count == 3:
                    board[i][j] += 2

        for i in range(n):
            for j in range(m):
                board[i][j] = int(board[i][j] / 2)

